package com.xuyuli.service;

import com.xuyuli.dao.LoginLogDao;
import com.xuyuli.dao.UserDao;
import com.xuyuli.domain.LoginLog;
import com.xuyuli.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;

    public boolean hasMatchUser(String userId,String password){
        int matchCount = userDao.getMatchCount(userId, password);
        return matchCount > 0;
    }

    public boolean hasMatchUserId(String userId){
        int matchCount = userDao.getMatchIdCount(userId);
        return matchCount > 0;
    }


    public User findUserByUserId(String userId){
        return userDao.findUserByUserId(userId);
    }

    @Transactional
    public void loginSuccess(User user){
        LoginLog loginLog = new LoginLog();
        loginLog.setUserId(user.getUserId());
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();
        loginLog.setLoginDate(sdf.format(date));
        loginLog.setOperating("login");
        loginLogDao.insertLoginLog(loginLog);
        System.out.println(user.toString());
    }


    public void register(String userId, String userName, String password) {
        userDao.register(userId,userName,password);
    }
}
